CREATE OR REPLACE package body db_modele_occurence as

	PROCEDURE addRendezVous(ev_usr in varchar2, ev_titre in varchar2, ev_objet in varchar2, ev_lieu in varchar2, ev_date_deb in varchar2, ev_date_fin in varchar2, date_deb in varchar2, date_fin in varchar2, ev_complet in number, ev_importance in varchar2) AS
	x1 date;
	x2 date;
	
	BEGIN

		IF ev_complet = 1 THEN
			x1 := null;
			x2 := null;
		ELSE
			x1 := to_date(date_deb, 'DD/MM/YYYY HH24:MI:SS');
			x2 := to_date(date_fin, 'DD/MM/YYYY HH24:MI:SS');
		END IF;
		
		INSERT INTO RENDEZ_VOUS 
		( 
			  RDV_ID
			, UTI_LOGIN
			, PER_ID_HER_1
			, RDV_TITRE
			, RDV_OBJET
			, RDV_LIEU
			, PER_ID
			, EVU_DATE_DEB
			, EVU_DATE_FIN
			, EVU_HEURE_DEB
			, EVU_HEURE_FIN
			, EVU_JOUR_COMPLET
			, EVU_IMPORTANCE
			, EVU_DUREE
		)
		VALUES 
		(
			  SEQ_RDV_ID.nextval
			, ev_usr
			, 0
			, ev_titre
			, ev_objet
			, ev_lieu
			, null
			, to_date(ev_date_deb, 'DD/MM/YYYY HH24:MI:SS')
			, to_date(ev_date_fin, 'DD/MM/YYYY HH24:MI:SS')
			, x1
			, x2
			, ev_complet
			, ev_importance
			, null
		)
	;
	END;
	
	PROCEDURE addOccurence(ev_lieu in varchar2, ev_date_deb in varchar2, ev_date_fin in varchar2, date_deb in varchar2, date_fin in varchar2, ev_complet in number, ev_importance in varchar2) AS
	x1 date;
	x2 date;
	BEGIN
		IF ev_complet = 1 THEN
			x1 := null;
			x2 := null;
		ELSE
			x1 := to_date(date_deb, 'DD/MM/YYYY HH24:MI:SS');
			x2 := to_date(date_fin, 'DD/MM/YYYY HH24:MI:SS');
		END IF;

		INSERT INTO OCCURENCE 
		( 
			  RDV_ID
			, OCC_ID
			, OCC_LIEU_EVENT
			, OCC_DATE_DEBUT
			, OCC_DATE_FIN
			, OCC_HEURE_DEBUT
			, OCC_HEURE_FIN
			, OCC_IMP_EVENT
			, OCC_JOUR_ENTIER
			, OCC_DUREE
		)
		VALUES 
		(
			  SEQ_RDV_ID.currval
			, SEQ_OCC_ID.nextval
			, ev_lieu
			, to_date(ev_date_deb, 'DD/MM/YYYY HH24:MI:SS')
			, to_date(ev_date_fin, 'DD/MM/YYYY HH24:MI:SS')
			, x1
			, x2
			, ev_importance
			, ev_complet
			, null
		)
	;
	
	END;
	
	PROCEDURE addInvit(uti_login in varchar2) AS
	BEGIN
		INSERT INTO ETRE_INVITE_OCCURENCE 
		(
			  UTI_LOGIN
			, RDV_ID
			, OCC_ID
			, INV_OCCU_PARTICIPE
		)
		VALUES 
		(
			  uti_login
			, SEQ_RDV_ID.currval
			, SEQ_OCC_ID.currval
			, null
		)
	;
	
	END;
	
	PROCEDURE addInvit2(uti_login in varchar2, xocc_id in number, xrdv_id in number) AS
	BEGIN
		INSERT INTO ETRE_INVITE_OCCURENCE 
		(
			  UTI_LOGIN
			, RDV_ID
			, OCC_ID
			, INV_OCCU_PARTICIPE
		)
		VALUES 
		(
			  uti_login
			, xrdv_id
			, xocc_id
			, null
		)
	;
	
	END;
	
	
	PROCEDURE deleteOccu(xrdv_id rendez_vous.rdv_id%type, xocc_id occurence.occ_id%type) AS
	BEGIN
		DELETE FROM RAPPELS WHERE RDV_ID = xrdv_id;
		DELETE FROM ETRE_INVITE_OCCURENCE WHERE RDV_ID = xrdv_id AND OCC_ID = xocc_id;
		DELETE FROM OCCURENCE WHERE OCC_ID=xocc_id AND RDV_ID=xrdv_id;
		DELETE FROM RENDEZ_VOUS WHERE RDV_ID = xrdv_id;
	END;
	
	PROCEDURE delInvit(util in varchar2, xocc_id in number, xrdv_id in number) AS
	BEGIN
		DELETE FROM ETRE_INVITE_OCCURENCE WHERE RDV_ID = xrdv_id AND OCC_ID = xocc_id AND UTI_LOGIN = util;
	END;
	
	PROCEDURE updateOccu(xocc_id in number, xrdv_id in number, ev_titre in varchar2, ev_objet in varchar2, ev_lieu in varchar2, ev_date_deb in varchar2, ev_date_fin in varchar2, ev_complet in number, ev_heure_deb in varchar2 default '', ev_heure_fin in varchar2 default '', ev_importance in varchar2) AS
	temp1 varchar2(30);
	temp2 varchar2(30);
	x1 date;
	x2 date;
	BEGIN
		IF ev_complet = 1 THEN
			x1 := null;
			x2 := null;
		ELSE
			temp1 := ev_date_deb || ' ' ||  ev_heure_deb;
			temp2 := ev_date_fin || ' ' ||  ev_heure_fin;
			x1 := to_date(temp1, 'DD/MM/YYYY HH24:MI:SS');
			x2 := to_date(temp2, 'DD/MM/YYYY HH24:MI:SS');
		END IF;
		
		update
			occurence
		set       
			  OCC_LIEU_EVENT = ev_lieu
			, OCC_DATE_DEBUT = to_date(ev_date_deb, 'DD/MM/YYYY HH24:MI:SS')
			, OCC_DATE_FIN = to_date(ev_date_fin, 'DD/MM/YYYY HH24:MI:SS')
			, OCC_HEURE_DEBUT = x1
			, OCC_HEURE_FIN = x2
			, OCC_IMP_EVENT = ev_importance
			, OCC_JOUR_ENTIER = ev_complet
		where 
			occ_id = xocc_id
		AND 
			rdv_id = xrdv_id
		;
		
		update
			rendez_vous
		set         
			  RDV_TITRE = ev_titre             
			, RDV_OBJET = ev_objet                              
			, EVU_DATE_DEB = to_date(ev_date_deb, 'DD/MM/YYYY HH24:MI:SS')           
			, EVU_DATE_FIN = to_date(ev_date_fin, 'DD/MM/YYYY HH24:MI:SS')          
			, EVU_HEURE_DEB = x1         
			, EVU_HEURE_FIN = x2
			, EVU_JOUR_COMPLET = ev_complet
			, EVU_IMPORTANCE = ev_importance                    
		where
			rdv_id = xrdv_id
		;
	END;


end db_modele_occurence;
/

commit;

show errors;
